package pwd.allen.util.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 自定义BigDecimal类型的序列化
 * @author pwdan
 * @create 2024-04-17 15:33
 **/
public class CustomBigDecimalSerializer extends JsonSerializer<BigDecimal> {
    @Override
    public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        // 设置格式化模式和小数位数
        DecimalFormat df = new DecimalFormat("#0.00");
        String formatted = df.format(bigDecimal);
        jsonGenerator.writeString(formatted);
    }
}
